<?php
namespace common\models;

use Yii;

class HttprequestLog extends \yii\db\ActiveRecord
{
    public static function getDb()
    {
        return Yii::$app->get("common_db");
    }
    
    public static function tableName()
    {
        return '{{%httprequest_log}}';
    }

    public function rules()
    {
        return [
            ['app_info_id', 'required', 'message' => 'app_info_id不能为空', 'on' => 'add'],
            ['app_info_id', 'integer', 'message' => 'app_info_id必须为数字，或数字字符串', 
                            // 'min'=>1, 'tooSmall' => 'app_info_id最小存储1', 
                            'max'=>65535, 'tooBig' => 'app_info_id最大存储65535'],
            ['request_url', 'default', 'value' => ''],
            ['request_url', 'string', 'message' => 'request_url必须是字符串', 
                            'max' => 512, 'tooLong' => 'request_url最长512位'],
            ['request_data', 'default', 'value' => ''],
            ['request_data', 'string', 'message' => 'request_data必须是字符串'],
            ['request_cookies', 'default', 'value' => ''],
            ['request_cookies', 'string', 'message' => 'request_cookies必须是字符串'],
            ['request_ip', 'string', 'message' => 'request_ip必须是字符串', 
                            'max' => 64, 'tooLong' => 'request_ip最长64位'], 
            ['request_way', 'default', 'value' => 1],
            ['request_way', 'integer', 'message' => 'request_way必须为数字，或数字字符串'],
            ['request_way', 'in', 'range' => array(1, 2), 'message' => 'request_way值必须在1,2中才选择'],
            ['requesttime', 'default', 'value' => 0],
            ['requesttime', 'integer', 'message' => 'requesttime必须为数字，或数字字符串', 
                   'min'=>0, 'tooSmall' => 'requesttime最小存储0', 
                   'max'=>18446744073709551615, 'tooBig' => 'requesttime最大存储18446744073709551615'],
            ['response_url', 'default', 'value' => ''],
            ['response_url', 'string', 'message' => 'response_url必须是字符串', 
                             'max' => 512, 'tooLong' => 'response_url最长512位'],
            ['response_data', 'default', 'value' => ''],
            ['response_data', 'string', 'message' => 'response_data必须是字符串'],
            ['responsetime', 'default', 'value' => 0],
            ['responsetime', 'integer', 'message' => 'responsetime必须为数字，或数字字符串', 
                   'min'=>0, 'tooSmall' => 'responsetime最小存储0', 
                   'max'=>18446744073709551615, 'tooBig' => 'responsetime最大存储18446744073709551615'],
            ['isok', 'default', 'value' => 1],
            ['isok', 'integer', 'message' => 'isok必须为数字，或数字字符串'],
            ['isok', 'in', 'range' => array(1, 2), 'message' => 'isok值必须在1,2中才选择'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /*添加HTTP请求日志*/
    public static function add($data)
    {
        $data = array(
            "HttprequestLog" => $data
        );
        // P($data);
        $httprequestLog = new HttprequestLog();
        if($httprequestLog->load($data) and $httprequestLog->validate()){
            if($httprequestLog->save(false)){
                $id = $httprequestLog->getPrimaryKey();
                return $id;
                // return true;
            }
        }else{
            if($httprequestLog->hasErrors()){
                echo 'error';
                P($httprequestLog->getErrors());
            }
        }
        return false;
    }



}
